//
//
//
//  
//  Created by wulanzhou on 16-3-7.
//  Copyright (c) 2016年. All rights reserved.
//

#import "WBRequestObj.h"
#import "AFHTTPRequestOperationManager.h"
#import "XmlParseHelper.h"

@interface WBRequestObj ()
@property (nonatomic,strong) AFHTTPRequestOperation *requestHandle;
@end


@implementation WBRequestObj

@synthesize requestHandle;
@synthesize delegate;


- (void)dealloc
{
    
    self.delegate = nil;
    if (self.requestHandle)
    {
        //[self.requestHandle.operationQueue cancelAllOperations];
        [self.requestHandle cancel];
    }
    self.requestHandle = nil;
   
}
/**
 *  单例
 */
+ (WBRequestObj*)sharedInstance
{
    static dispatch_once_t  onceToken;
    static WBRequestObj * sSharedInstance;
    dispatch_once(&onceToken, ^{
        sSharedInstance = [[WBRequestObj alloc] init];
    });
    return sSharedInstance;
}
/**
 *  request请求
 *
 *  @param args        请求参数
 *  @param reqDelegate 请求结果代理
 *
 */
- (void)requestService:(WBRequestArgs*)args
              delegate:(id<CZRequestObjDelegate>)reqDelegate{
    self.requestArgs=args;
    self.delegate=reqDelegate;
    self.requestHandle=args.request;
    
    __block WBRequestObj *_objSelf=self;
    
    [self.requestHandle setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        //NSString *str=[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

        
        
        WBResponse *mod=[[WBResponse alloc] init];
        
        //xml解析处理
        NSString *xml=[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSString *replaceStr=[NSString stringWithFormat:@"xmlns=\"%@\"",args.serviceNameSpace];
        NSString *filterXml=[xml stringByReplacingOccurrencesOfString:replaceStr withString:@""];
        XmlParseHelper *parse=[[XmlParseHelper alloc] initWithData:filterXml];
        mod.content=[parse soapMessageResultXml:args.methodName]; //解析xml
        
        //原始请求结果
        mod.xml=xml;

        if ([responseObject isKindOfClass:[NSData class]]) {

            mod.success=YES;
            mod.code=@"200";
            mod.errMsg=@"";
            if(_objSelf.delegate&&[_objSelf.delegate respondsToSelector:@selector(CZRequestFinished:resultResponse:)])
            {
                [_objSelf.delegate CZRequestFinished:_objSelf resultResponse:mod];
            }
        }else{
            mod.success=NO;
            mod.code=@"-999";
            mod.errMsg=@"返回数据为空";
        }
        if(_objSelf.delegate&&[_objSelf.delegate respondsToSelector:@selector(CZRequestFinished:resultResponse:)])
        {
            [_objSelf.delegate CZRequestFinished:_objSelf resultResponse:mod];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        WBResponse *mod=[[WBResponse alloc] init];
        mod.content=@"";
        mod.xml=@"";
        mod.success=NO;
        mod.code=@"9999";
        mod.errMsg=@"服务器异常!";
        
        if(_objSelf.delegate&&[_objSelf.delegate respondsToSelector:@selector(CZRequestFinished:resultResponse:)])
        {
            [_objSelf.delegate CZRequestFinished:_objSelf resultResponse:mod];
        }
       
    }];
    [self.requestHandle start];
}
/**
 *  停止请求
 */
- (void)stopRequest
{
    if (self.requestHandle) {
       //[self.requestHandle.operationQueue cancelAllOperations];
       [self.requestHandle cancel];
    }
}
@end
